import axios from "axios";


const server = axios.create({
    // baseURL: 'http://192.168.10.44:8070/',
    // baseURL: 'http://39.100.38.199:8070',
    baseURL: import.meta.env.VITE_BASE_URL,
    timeout: 300000,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8' }
})

server.interceptors.request.use(async config => {
    let data = {}

    config.headers = { 'Content-Type': 'application/json;' }
    config.headers.osVersion = 2
    const token = await localStorage.getItem('token')
    if (token && token !== 'undefined') {
        config.headers['x-token'] = JSON.parse(token)
    }
    if (config.data) {
        data = config.data
        if(data.file){
            config.headers = {
                'Content-Type': 'multipart/form-data;'
            }
            if (token && token !== 'undefined') {
                config.headers['x-token'] = JSON.parse(token)
            }
            let formData = new FormData()
            formData.append('file',data.file)
            config.data = formData
        }else{
            config.data = JSON.stringify(data)
        }
    }
    return config
}, function (err) {
    Promise.reject(err)
})


server.interceptors.response.use(async response => {
    if (response.data.code === '10004') {
        localStorage.removeItem("token")
        window.location.href = '/login'
    }
    return response
}
)
export default server